package com.gu.qqdemo.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gu.qqdemo.Bean.FeatureBean;
import com.gu.qqdemo.R;

import java.util.List;


public class RvNewsAdapter extends RecyclerView.Adapter<RvNewsAdapter.ViewHolder> {

    private List<FeatureBean> listData;

    public RvNewsAdapter(List<FeatureBean> listData) {
        this.listData = listData;
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_news, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        FeatureBean bean = listData.get(position);
        holder.setData(bean);
    }


    // RecyclerView最核心的思想，是用ViewHolder进行维护
    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv;
        private TextView tv;

        public ViewHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            tv = itemView.findViewById(R.id.tv);
        }

        public void setData(FeatureBean bean) {
            tv.setText(bean.getName());
        }

    }
}
